package com.reggie.acl.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.reggie.common.util.DateTimeUtils;
import com.reggie.common.util.ReggieContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

/**
 * 自定义元数据处理器
 *
 * @author yth
 * @since 2023-02-05-15:22
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        if (log.isDebugEnabled()) {
            log.debug("insertFill()");
        }
        this.setFieldValByName("createTime", DateTimeUtils.utcNow(), metaObject);
        this.setFieldValByName("updateTime", DateTimeUtils.utcNow(), metaObject);
        this.setFieldValByName("createUser", ReggieContext.getInstance().getEmployeeId(), metaObject);
        this.setFieldValByName("updateUser", ReggieContext.getInstance().getEmployeeId(), metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        if (log.isDebugEnabled()) {
            log.debug("updateFill()");
        }
        this.setFieldValByName("updateTime", DateTimeUtils.utcNow(), metaObject);
        this.setFieldValByName("updateUser", ReggieContext.getInstance().getEmployeeId(), metaObject);
    }
}
